package com.logictech.lims.web.back;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ContextHolderUtils;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.logictech.activity.system.vo.LoginUser;
import com.logictech.lims.dto.ExperimentMaterialDto;
import com.logictech.lims.entity.Experiment;
import com.logictech.lims.entity.ExperimentMaterial;
import com.logictech.lims.service.ExperimentMaterialService;
import com.logictech.lims.service.ExperimentService;

/**
 * 描述：</b>ExperimentMaterialController<br>
 * 实验
 * 
 * @author guoqi
 * @since：2017年04月22日 10时21分18秒 星期六
 * @version:1.0
 */
@Controller
@RequestMapping("/lims/back/experimentMaterial")
public class ExperimentMaterialController extends BaseController {
	@Autowired
	private ExperimentMaterialService experimentMaterialService;

	@Autowired
	private ExperimentService experimentService;
	
	/**
	 * 列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public void list(@ModelAttribute ExperimentMaterial query, HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
		PageQuery<ExperimentMaterial> pageQuery = new PageQuery<ExperimentMaterial>();
		pageQuery.setPageNo(pageNo);
		pageQuery.setPageSize(pageSize);
		VelocityContext velocityContext = new VelocityContext();
		pageQuery.setQuery(query);
		velocityContext.put("experimentMaterial", query);
		velocityContext.put("pageInfos",
				SystemTools.convertPaginatedList(experimentMaterialService.queryPageList(pageQuery)));
		String viewName = "lims/back/experimentMaterial-list.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "toDetail", method = RequestMethod.GET)
	public void toDetail(@RequestParam(required = true, value = "id") String id,
			HttpServletResponse response, HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "lims/back/experimentMaterial-detail.vm";
		ExperimentMaterial experimentMaterial = experimentMaterialService.queryById(id);
		velocityContext.put("experimentMaterial", experimentMaterial);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toAdd", method = { RequestMethod.GET, RequestMethod.POST })
	public void toAdd(@RequestParam(required = true, value = "experimentId") String experimentId,HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "lims/back/experimentMaterial-add.vm";
		ExperimentMaterial experimentMaterial = new ExperimentMaterial();
		experimentMaterial.setExperimentId(Integer.parseInt(experimentId));
		velocityContext.put("experimentMaterial", experimentMaterial);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 保存信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doAdd", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public AjaxJson doAdd(@ModelAttribute ExperimentMaterialDto experimentMaterialDto) {
		AjaxJson j = new AjaxJson();
		try {
			Experiment experiment = experimentService.queryById(experimentMaterialDto.getExperimentId().toString());
			experimentMaterialDto.setExperiment(experiment.getName());
			
			ExperimentMaterial experimentMaterial = new ExperimentMaterial();
			
			BeanUtils.copyProperties(experimentMaterialDto, experimentMaterial);
			if("1".equals(experimentMaterial.getMaterialType())){
				experimentMaterial.setMaterialId(experimentMaterialDto.getReagentTypeId());
				experimentMaterial.setMaterialName(experimentMaterialDto.getReagentType());
			}else if("2".equals(experimentMaterial.getMaterialType())){
				experimentMaterial.setMaterialId(experimentMaterialDto.getPreparationTypeId());
				experimentMaterial.setMaterialName(experimentMaterialDto.getPreparationType());
			}
			LoginUser user = (LoginUser) ContextHolderUtils.getSession().getAttribute("OPERATE_WEB_LOGIN_USER");
			experimentMaterial.setCreateUser(user.getId().toString());
			experimentMaterial.setCreateTime(new Date());
			experimentMaterial.setUpdateUser(user.getId().toString());
			experimentMaterial.setUpdateTime(new Date());
			experimentMaterialService.doAdd(experimentMaterial);
			j.setMsg("保存成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("保存失败");
		}
		return j;
	}

	/**
	 * 跳转到编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "toEdit", method = RequestMethod.GET)
	public void toEdit(@RequestParam(required = true, value = "id") String id, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		ExperimentMaterialDto experimentMaterialDto = experimentMaterialService.queryDtoById(id);
		if("1".equals(experimentMaterialDto.getMaterialType())){
			experimentMaterialDto.setReagentTypeId(experimentMaterialDto.getMaterialId());
		}else if("2".equals(experimentMaterialDto.getMaterialType())){
			experimentMaterialDto.setPreparationTypeId(experimentMaterialDto.getMaterialId());
		}
		
		velocityContext.put("experimentMaterial", experimentMaterialDto);
		String viewName = "lims/back/experimentMaterial-edit.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doEdit", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public AjaxJson doEdit(@ModelAttribute ExperimentMaterialDto experimentMaterial) {
		AjaxJson j = new AjaxJson();
		try {
			Experiment experiment = experimentService.queryById(experimentMaterial.getExperimentId().toString());
			experimentMaterial.setExperiment(experiment.getName());
			
			if("1".equals(experimentMaterial.getMaterialType())){
				experimentMaterial.setMaterialId(experimentMaterial.getReagentTypeId());
				experimentMaterial.setMaterialName(experimentMaterial.getReagentType());
			}else if("2".equals(experimentMaterial.getMaterialType())){
				experimentMaterial.setMaterialId(experimentMaterial.getPreparationTypeId());
				experimentMaterial.setMaterialName(experimentMaterial.getPreparationType());
			}
			
			experimentMaterialService.doEdit(experimentMaterial);
			j.setMsg("编辑成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("编辑失败");
			log.error(e.getStackTrace().toString());
		}
		return j;
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "doDelete", method = RequestMethod.GET)
	@ResponseBody
	public AjaxJson doDelete(@RequestParam(required = true, value = "id") String id) {
		AjaxJson j = new AjaxJson();
		try {
			experimentMaterialService.doDelete(id);
			j.setMsg("删除成功");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("删除失败");
		}
		return j;
	}

}
